package net.asfun.jvalog.domain;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.jdo.Query;

import net.asfun.jvalog.except.InteractException;
import net.asfun.jvalog.po.Relation;
import net.asfun.jvalog.po.Sort;
import net.asfun.jvalog.service.Jdoer;
import net.asfun.jvalog.service.JdoerFactory;
import net.asfun.jvalog.task.UpdateManager;
import net.asfun.jvalog.view.PagerInfo;
import net.asfun.jvalog.view.SiteInfo;

public class Category {
	
	private Sort sort; 
	
	private List<Relation> posts;
	
	public Category() {
		sort = new Sort();
	}
	
	private Category(Sort as) {
		if ( as == null) {
			throw new InteractException("Category not found");
		}
		sort = as;
	}

	public static List<Category> list(PagerInfo pager) {
		Jdoer<Sort> dbs = JdoerFactory.getSortJdo();
		pager.setTotal(dbs.all().size());
		Query query = dbs.prepareQuery();
		query.setOrdering("order");
		query.setRange(pager.getStart(), pager.getEnd());
		Collection<Sort> sorts = dbs.more(query);
		List<Category> list = new ArrayList<Category>();
		for(Sort sort : sorts) {
			list.add(new Category(sort));
		}
		return list;
	}

	public static void remove(Long id) {
		Jdoer<Sort> dbs = JdoerFactory.getSortJdo();
		Sort sort = dbs.load(id);
		if ( sort != null ) {
			Unifier.unsetUnique(sort.getKey(), "slogan");
			dbs.delete(Bridge.getRelationsByRight(sort.getKey(), Bridge.POST_CATEGORY));
			dbs.delete(sort);
			UpdateManager.enableCacheTask();
		}
	}

	public void setOrder(int order) {
		sort.setOrder(order);
	}
	
	public int getOrder() {
		return sort.getOrder();
	}

	public void setName(String name) {
		sort.setName(name);
	}
	
	public String getName() {
		return sort.getName();
	}

	public void setSlogan(String slogan) {
		sort.setSlogan(slogan);
	}
	
	public String getSlogan() {
		return sort.getSlogan();
	}
	
	public Long getId() {
		return sort.getKey().getId();
	}
	
	public int getPostCount() {
		return Counter.getCount(sort.getKey());
	}
	
	private void setPosts() {
		if ( null == posts ) {
			posts = Bridge.getRelationsByRight(sort.getKey(), Bridge.POST_CATEGORY);
		}
	}
	
	public List<Post> listPublishedPosts(PagerInfo pager) {
		pager.setTotal(getPostCount());
		List<Post> list = new ArrayList<Post>();
		int j=0;
		setPosts();
		for(int i=posts.size(); i>0; ) {
			Post post = Post.load(posts.get(--i).getLeft().getId());
			if ( post != null && post.isPublished() ) {
				//paging
				if ( j >= pager.getStart() && 
						j < pager.getEnd() ) {
					list.add(post);
				}
				j++;
			}
		}
		if ( list.isEmpty() && pager.getIndex() > 1) {
			throw new InteractException("Category pager no record");
		}
		return list;
	}

	public void save() {
		//make sure slogan unique
		if ( ! Unifier.isUnique("Sort", "slogan", sort.getSlogan()) ) {
			sort.setSlogan(sort.getSlogan() + "~1");
			this.save();
			return;
		}
		Jdoer<Sort> dbs = JdoerFactory.getSortJdo();
		dbs.save(sort);
		Unifier.setUnique(sort.getKey(), "slogan", sort.getSlogan());
		UpdateManager.enableCacheTask();
	}

	public static Category load(Long id) {
		Jdoer<Sort> dbs = JdoerFactory.getSortJdo();
		Category cat = new Category(dbs.load(id));
		return cat;
	}

	public static List<Category> listAll() {
		Jdoer<Sort> dbs = JdoerFactory.getSortJdo();
		Query query = dbs.prepareQuery();
		query.setOrdering("order");
		Collection<Sort> sorts = dbs.more(query);
		List<Category> list = new ArrayList<Category>();
		for(Sort sort : sorts) {
			list.add(new Category(sort));
		}
		return list;
	}

	public void update() {
		//make sure slogan unique
		if ( ! Unifier.isUnique(sort.getKey(), "slogan", sort.getSlogan()) ) {
			sort.setSlogan(sort.getSlogan() + "~1");
			this.update();
			return;
		}
		Unifier.setUnique(sort.getKey(), "slogan", sort.getSlogan());
		UpdateManager.enableCacheTask();
	}

	protected static Sort locate(Long id) {
		return load(id).sort;
	}

	public static Category load(String slogan) {
		Jdoer<Sort> dbs = JdoerFactory.getSortJdo();
		Query query = dbs.prepareQuery();
		query.setFilter("slogan == slog");
		query.declareParameters("String slog");
		return new Category(dbs.unique(query, slogan));
	}

	public String getLink() {
		return SiteInfo.getInstance().getUrl() + "category/" + sort.getSlogan();
	}

}
